﻿using C556Backside.Models;
using Microsoft.EntityFrameworkCore;

namespace C556Backside.Services;

public class C556DBContext : DbContext
{
    public DbSet<VoluntaryModel> Voluntary { get; set; }
    public DbSet<VoluntaryActivityModel> VoluntaryActivities { get; set; }
    public DbSet<VAandVRelationshipsModel> VAVRelationshipsTable { get; set; }
    public DbSet<OrganizationModel> Organizations { get; set; }
    public DbSet<ManagerModel> Managers { get; set; }
    public DbSet<ActivityTypeModel> ActivityTypeModels { get; set; }
    public DbSet<ActivityReviewModel> ActivityReviews { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
        //填写连接字符串（针对Sql server的连接字符串，在.NET Core中可以连接多种数据库）
        optionsBuilder.UseSqlServer("Server=rm-m5ejgpk5hq9m228lufo.sqlserver.rds.aliyuncs.com, 3433;Database=C556Non-spatialdata;User ID=duan;Password=dxb1314@;Trusted_Connection=False;Encrypt=False");
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        //配置主外键关系
        modelBuilder.Entity<VoluntaryModel>().HasMany(e => e.VAandVRelationshipsModels).WithOne(e => e.VoluntaryModel).HasForeignKey(e => e.VoluntaryID);
        modelBuilder.Entity<VoluntaryActivityModel>().HasMany(e => e.VAandVRelationshipsModels).WithOne(e => e.VoluntaryActivityModel).HasForeignKey(e => e.ActivityID);
        modelBuilder.Entity<OrganizationModel>().HasMany(e => e.VoluntaryActivities).WithOne(e => e.OrganizationModel).HasForeignKey(e => e.OrganizationID);
        modelBuilder.Entity<ActivityTypeModel>().HasMany(e => e.VoluntaryActivities).WithOne(e => e.ActivityTypeModel).HasForeignKey(e => e.ActivityTypeID);
        modelBuilder.Entity<VAandVRelationshipsModel>().HasMany(e => e.ActivityReviews).WithOne(e => e.VAandVRelationships).HasForeignKey(e => e.VAAndVID);
        //配置种子数据
        modelBuilder.Entity<VoluntaryModel>().HasData(new VoluntaryModel
        {
            VoluntaryName = "Test1",
            VoluntaryPassword = "666666",
            VoluntaryTelephone = "15007473346",
            Province = "山东省",
            City = "济南市",
            Area = "长清区"
        });

        var initialOrg = new OrganizationModel
        {
            OrganizationName = "Test1",
            OrganizationPassword = "666666",
            OrganizationTelephone = "135382223769",
            Province = "广东省",
            City = "佛山市",
            Area = "顺德区"
        };

        var manager = new ManagerModel
        {
            Name = "Test1",
            Password = "6666666"
        };
        modelBuilder.Entity<ManagerModel>().HasData(manager);
        modelBuilder.Entity<OrganizationModel>().HasData(initialOrg);

        //var initialAT = GetActivityTypeInitialContent();
        //modelBuilder.Entity<ActivityTypeModel>().HasData(initialAT);

        //var initialVA = GetInitialVoluntaryActivityModels(initialOrg.OrganizationID, initialAT.ToArray()[0].ActivityTypeID);
        //modelBuilder.Entity<VoluntaryActivityModel>().HasData(initialVA);
    }
    //private List<ActivityTypeModel> GetActivityTypeInitialContent()
    //{
    //    return new List<ActivityTypeModel> {
    //        new ActivityTypeModel{ActivityTypeName="扶贫开发"},
    //        new ActivityTypeModel{ActivityTypeName="社区建设"},
    //        new ActivityTypeModel{ActivityTypeName="大型赛会"},
    //        new ActivityTypeModel{ActivityTypeName="应急救助"},
    //        new ActivityTypeModel{ActivityTypeName="海外服务"}
    //    };
    //}
    //private List<VoluntaryActivityModel> GetInitialVoluntaryActivityModels(string orgID, string aTID)
    //{
    //    return new List<VoluntaryActivityModel> {
    //        new VoluntaryActivityModel{ ActivityID="D4880CB1-361D-CEF2-FCFB-65E5A9453AF1", ActivityName="山东交通学院", ActivityCompleteTime="",Province="山东省",City="济南市",Area="长清区",DetailedAddress="",ActivityTypeID=aTID,OrganizationID=orgID},
    //        new VoluntaryActivityModel{ ActivityID="54D1D5D8-F90F-9E12-5E27-B6A592E375AF", ActivityName="园博园",  ActivityCompleteTime="",Province="山东省",City="济南市",Area="长清区" ,DetailedAddress="",ActivityTypeID=aTID,OrganizationID=orgID},
    //        new VoluntaryActivityModel{ ActivityID="77649FFF-B7FB-4200-C807-5359D264A15D", ActivityName="园博园会展中心", ActivityCompleteTime="", Province="山东省",City="济南市",Area="长清区" ,DetailedAddress="",ActivityTypeID=aTID,OrganizationID=orgID},
    //        new VoluntaryActivityModel{ ActivityID="AC837197-BB0E-7365-9CE1-55BC0E56A789", ActivityName="齐鲁工业大学", ActivityCompleteTime="", Province="山东省",City="济南市",Area="长清区",DetailedAddress="", ActivityTypeID=aTID,OrganizationID=orgID},
    //        new VoluntaryActivityModel{ ActivityID="D23CE4D1-2583-F20D-51C2-8FF4875C24A7", ActivityName="山东中医药大学", ActivityCompleteTime="", Province="山东省",City="济南市",Area="长清区" ,DetailedAddress = "", ActivityTypeID=aTID,OrganizationID=orgID}
    //    };
    //}
}
